package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.dao.RoleMenuDao;
import com.aaa.vegetablemarketexpress.service.RoleMenuService;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @FileName: RoleMenuController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/9 15:40
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("roleMenu")
public class RoleMenuController {
    @Resource
    private RoleMenuService roleMenuService;
    /**
     * 为角色分配菜单权限
     */
    @PostMapping("assignMenus")
    public Result assignMenusToRole(@RequestParam Long roleId,@RequestBody List<Long> menuIds) {
            roleMenuService.assignMenusToRole(roleId, menuIds);
            return ResultUtil.success("菜单权限分配成功");
    }

    /**
     * 获取角色拥有的菜单ID列表
     */
    @GetMapping("MenuIdsByRoleId")
    public Result<List<Long>> MenuIdsByRoleId(Long roleId) {

            List<Long> menuIds = roleMenuService.getMenuIdsByRoleId(roleId);
            return ResultUtil.success(menuIds);

    }

    /**
     * 删除角色的所有菜单权限
     */
    @DeleteMapping("removeAll")
    public Result<String> removeAllMenusFromRole(Long roleId) {
            roleMenuService.removeAllMenusFromRole(roleId);
            return ResultUtil.success("已移除角色的所有菜单权限");
    }

    /**
     * 检查角色是否有某个菜单权限
     */
    @GetMapping("checkPermission")
    public Result<Boolean> hasMenuPermission( Long roleId,Long menuId) {

            boolean hasPermission = roleMenuService.hasMenuPermission(roleId, menuId);
            return ResultUtil.success(hasPermission);

    }
}
